<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class Thread::Queue - RDoc Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link"><a href="../Object.html">Object</a>
</div>

        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-new">::new</a>
    <li ><a href="#method-i-3C-3C">#&lt;&lt;</a>
    <li ><a href="#method-i-clear">#clear</a>
    <li ><a href="#method-i-close">#close</a>
    <li ><a href="#method-i-closed-3F">#closed?</a>
    <li ><a href="#method-i-deq">#deq</a>
    <li ><a href="#method-i-empty-3F">#empty?</a>
    <li ><a href="#method-i-enq">#enq</a>
    <li ><a href="#method-i-length">#length</a>
    <li ><a href="#method-i-num_waiting">#num_waiting</a>
    <li ><a href="#method-i-pop">#pop</a>
    <li ><a href="#method-i-push">#push</a>
    <li ><a href="#method-i-shift">#shift</a>
    <li ><a href="#method-i-size">#size</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-Thread::Queue">
    <h1 id="class-Thread::Queue" class="class">
      class Thread::Queue
    </h1>

    <section class="description">
    
<p>The <a href="Queue.html"><code>Thread::Queue</code></a> class implements multi-producer, multi-consumer queues.  It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The <a href="Queue.html"><code>Thread::Queue</code></a> class implements all the required locking semantics.</p>

<p>The class implements FIFO (first in, first out) type of queue. In a FIFO queue, the first tasks added are the first retrieved.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">queue</span> = <span class="ruby-constant">Thread</span><span class="ruby-operator">::</span><span class="ruby-constant">Queue</span>.<span class="ruby-identifier">new</span>

<span class="ruby-identifier">producer</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span>
  <span class="ruby-value">5</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">sleep</span> <span class="ruby-identifier">rand</span>(<span class="ruby-identifier">i</span>) <span class="ruby-comment"># simulate expense</span>
    <span class="ruby-identifier">queue</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">i</span>
    <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{i} produced&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">consumer</span> = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">do</span>
  <span class="ruby-value">5</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">value</span> = <span class="ruby-identifier">queue</span>.<span class="ruby-identifier">pop</span>
    <span class="ruby-identifier">sleep</span> <span class="ruby-identifier">rand</span>(<span class="ruby-identifier">i</span><span class="ruby-operator">/</span><span class="ruby-value">2</span>) <span class="ruby-comment"># simulate expense</span>
    <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;consumed #{value}&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">consumer</span>.<span class="ruby-identifier">join</span>
</pre>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-new" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          Thread::Queue.new &rarr; empty_queue
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          Thread::Queue.new(enumerable) &rarr; queue
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Creates a new queue instance, optionally using the contents of an <code>enumerable</code> for its initial state.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">q</span> = <span class="ruby-constant">Thread</span><span class="ruby-operator">::</span><span class="ruby-constant">Queue</span>.<span class="ruby-identifier">new</span>
<span class="ruby-comment">#=&gt; #&lt;Thread::Queue:0x00007ff7501110d0&gt;</span>
<span class="ruby-identifier">q</span>.<span class="ruby-identifier">empty?</span>
<span class="ruby-comment">#=&gt; true</span>

<span class="ruby-identifier">q</span> = <span class="ruby-constant">Thread</span><span class="ruby-operator">::</span><span class="ruby-constant">Queue</span>.<span class="ruby-identifier">new</span>([<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>])
<span class="ruby-comment">#=&gt; #&lt;Thread::Queue:0x00007ff7500ec500&gt;</span>
<span class="ruby-identifier">q</span>.<span class="ruby-identifier">empty?</span>
<span class="ruby-comment">#=&gt; false</span>
<span class="ruby-identifier">q</span>.<span class="ruby-identifier">pop</span>
<span class="ruby-comment">#=&gt; 1</span>
</pre>

                              <div class="method-source-code" id="new-source">
            <pre>static VALUE
rb_queue_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE initial;
    struct rb_queue *q = queue_ptr(self);
    if ((argc = rb_scan_args(argc, argv, &quot;01&quot;, &amp;initial)) == 1) {
        initial = rb_to_array(initial);
    }
    RB_OBJ_WRITE(self, queue_list(q), ary_buf_new());
    ccan_list_head_init(queue_waitq(q));
    if (argc == 1) {
        rb_ary_concat(q-&gt;que, initial);
    }
    return self;
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-3C-3C" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          &lt;&lt;(object)
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Pushes the given <code>object</code> to the queue.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Queue.html#method-i-push">push</a>
                            </div>
                          </div>

                  <div id="method-i-clear" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">clear</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Removes all objects from the queue.</p>

                              <div class="method-source-code" id="clear-source">
            <pre>static VALUE
rb_queue_clear(VALUE self)
{
    struct rb_queue *q = queue_ptr(self);

    rb_ary_clear(check_array(self, q-&gt;que));
    return self;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-close" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          close
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Closes the queue. A closed queue cannot be re-opened.</p>

<p>After the call to close completes, the following are true:</p>
<ul><li>
<p><code>closed?</code> will return true</p>
</li><li>
<p><code>close</code> will be ignored.</p>
</li><li>
<p>calling enq/push/&lt;&lt; will raise a <code>ClosedQueueError</code>.</p>
</li><li>
<p>when <code>empty?</code> is false, calling deq/pop/shift will return an object from the queue as usual.</p>
</li><li>
<p>when <code>empty?</code> is true, deq(false) will not suspend the thread and will return nil. deq(true) will raise a <code>ThreadError</code>.</p>
</li></ul>

<p><a href="../ClosedQueueError.html"><code>ClosedQueueError</code></a> is inherited from <a href="../StopIteration.html"><code>StopIteration</code></a>, so that you can break loop block.</p>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">q</span> = <span class="ruby-constant">Thread</span><span class="ruby-operator">::</span><span class="ruby-constant">Queue</span>.<span class="ruby-identifier">new</span>
<span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span>{
  <span class="ruby-keyword">while</span> <span class="ruby-identifier">e</span> = <span class="ruby-identifier">q</span>.<span class="ruby-identifier">deq</span> <span class="ruby-comment"># wait for nil to break loop</span>
    <span class="ruby-comment"># ...</span>
  <span class="ruby-keyword">end</span>
}
<span class="ruby-identifier">q</span>.<span class="ruby-identifier">close</span>
</pre>

                              <div class="method-source-code" id="close-source">
            <pre>static VALUE
rb_queue_close(VALUE self)
{
    struct rb_queue *q = queue_ptr(self);

    if (!queue_closed_p(self)) {
        FL_SET(self, QUEUE_CLOSED);

        wakeup_all(queue_waitq(q));
    }

    return self;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-closed-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          closed?
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns <code>true</code> if the queue is closed.</p>

                              <div class="method-source-code" id="closed-3F-source">
            <pre>static VALUE
rb_queue_closed_p(VALUE self)
{
    return RBOOL(queue_closed_p(self));
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-deq" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">deq</span><span
                                class="method-args">(non_block = false, timeout: nil)</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Queue.html#method-i-pop">pop</a>
                            </div>
                          </div>

                  <div id="method-i-empty-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          empty?
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns <code>true</code> if the queue is empty.</p>

                              <div class="method-source-code" id="empty-3F-source">
            <pre>static VALUE
rb_queue_empty_p(VALUE self)
{
    return RBOOL(queue_length(self, queue_ptr(self)) == 0);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-enq" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          enq(object)
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Pushes the given <code>object</code> to the queue.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Queue.html#method-i-push">push</a>
                            </div>
                          </div>

                  <div id="method-i-length" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          length
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the length of the queue.</p>

                              <div class="method-source-code" id="length-source">
            <pre>static VALUE
rb_queue_length(VALUE self)
{
    return LONG2NUM(queue_length(self, queue_ptr(self)));
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Queue.html#method-i-size">size</a>
                            </div>

                          </div>

                  <div id="method-i-num_waiting" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">num_waiting</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the number of threads waiting on the queue.</p>

                              <div class="method-source-code" id="num_waiting-source">
            <pre>static VALUE
rb_queue_num_waiting(VALUE self)
{
    struct rb_queue *q = queue_ptr(self);

    return INT2NUM(q-&gt;num_waiting);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-pop" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          pop(non_block=false, timeout: nil)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Retrieves data from the queue.</p>

<p>If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If <code>non_block</code> is true, the thread isn’t suspended, and <code>ThreadError</code> is raised.</p>

<p>If <code>timeout</code> seconds have passed and no data is available <code>nil</code> is returned. If <code>timeout</code> is <code>0</code> it returns immediately.</p>

                              <div class="method-source-code" id="pop-source">
            <pre><span class="ruby-comment"># File ruby_3_3_0_preview2/thread_sync.rb, line 14</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pop</span>(<span class="ruby-identifier">non_block</span> = <span class="ruby-keyword">false</span>, <span class="ruby-value">timeout:</span> <span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">non_block</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">timeout</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;can&#39;t set a timeout if non_block is enabled&quot;</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-constant">Primitive</span>.<span class="ruby-identifier">rb_queue_pop</span>(<span class="ruby-identifier">non_block</span>, <span class="ruby-identifier">timeout</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Queue.html#method-i-deq">deq</a>, <a href="Queue.html#method-i-shift">shift</a>
                            </div>

                          </div>

                  <div id="method-i-push" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          push(object)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Pushes the given <code>object</code> to the queue.</p>

                              <div class="method-source-code" id="push-source">
            <pre>static VALUE
rb_queue_push(VALUE self, VALUE obj)
{
    return queue_do_push(self, queue_ptr(self), obj);
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Queue.html#method-i-enq">enq</a>, <a href="Queue.html#method-i-3C-3C">&lt;&lt;</a>
                            </div>

                          </div>

                  <div id="method-i-shift" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">shift</span><span
                                class="method-args">(non_block = false, timeout: nil)</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Queue.html#method-i-pop">pop</a>
                            </div>
                          </div>

                  <div id="method-i-size" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          size
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the length of the queue.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Queue.html#method-i-length">length</a>
                            </div>
                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>. </p><p><a href="https://jamesbritt.bandcamp.com/">Hack your world.  Feed your head.  Live curious</a>.</p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

